package com.example.demo01;

import com.alibaba.fastjson.JSON;
import com.example.demo01.entity.Result;
import com.example.demo01.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/demo01")
@Api(tags = "测试")
public class Controller {

    @GetMapping("/test")
    @ApiOperation("测试fastjson")
    public Object test() {
        Result resultA = new Result();
        Result result = new Result();
        result.setCode(200);
        result.setSuccess(true);
        result.setMsg("这是一个测试类");

        List<Result> resultList = new ArrayList<>();
        resultList.add(result);
        resultList.add(result);
        String jsonString = JSON.toJSONString(resultList);
        return jsonString;
    }

    @GetMapping("/copyFile")
    @ApiOperation("复制文件")
    public Object copyFile() {
        String sourcePath = "C:/Users/wangkl/Desktop/文档/RTI.docx";
        String destPath = "C:/Users/wangkl/Desktop/文档/test/RTI_" + new Date().getTime() + ".docx";
        sourcePath = "http://localhost:12802/demo2/1.png";
        destPath = "C:/Users/wangkl/Desktop/文档/test/RTI_" + new Date().getTime() + ".png";

        return FileUtil.copyFile(sourcePath, destPath);
    }

    @GetMapping("/copyFileByUrl")
    @ApiOperation("复制文件URL")
    public Object copyFileByUrl() {
        String remotePath = "http://localhost:12802/file_proxy";
        String fileName = "/demo/1.txt";
        InputStream ins = FileUtil.getInputStreamByUrl(remotePath, fileName);
        fileName = "/demo/" + new Date().getTime() + ".txt";
        OutputStream ous = FileUtil.getOutputStreamByUrl(remotePath, fileName);
        return FileUtil.copyFile(ins, ous);
    }

    @GetMapping("/printFile")
    @ApiOperation("打印文件")
    public Object printFile() {
        String remotePath = "http://localhost:12802/file_proxy";
        String fileName = "/demo/1.txt";
        FileUtil.printFile(remotePath, fileName);
        return "";
    }

}
